package com.sdy.dcsb.web.controller;

import com.sdy.common.model.Response;
import com.sdy.dcsb.biz.model.EsbAdapderHttp;
import com.sdy.dcsb.biz.model.EsbAdapderSoap;
import com.sdy.dcsb.biz.model.EsbAtomservice;
import com.sdy.dcsb.biz.service.EsbAdapderHttpService;
import com.sdy.dcsb.biz.service.EsbAdapderSoapService;
import com.sdy.dcsb.biz.service.EsbAtomServiceService;
import com.sdy.dcsb.biz.service.UserOperationLogService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Objects;
import static org.springframework.util.StringUtils.isEmpty;

@Slf4j
@RestController
@RequestMapping("/esbAdapderHttp")
@Api(tags = "原子服务适配信息接口")
public class EsbAdapderHttpController extends BaseController {
    @Autowired
    private EsbAdapderHttpService esbAdapderHttpService;
    @Autowired
    private EsbAdapderSoapService esbAdapderSoapService;
    @Autowired
    private UserOperationLogService userOperationLogService;
    @Autowired
    private EsbAtomServiceService esbAtomServiceDetailedService;

    @GetMapping("/selectEsbAdappderHttp")
    @ApiOperation("查询适配类型信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "原子服务id"),
    })
    public Response selectEsbAdappderHttp(EsbAtomservice esbAtomservice) {
        if (!isEmpty(esbAtomservice.getAdapterType()) && esbAtomservice.getAdapterType().equals("1")){
            return Response.success(esbAdapderHttpService.selectEsbAdapderHttp(esbAtomservice));
        }
        return Response.success(esbAdapderHttpService.selectEsbAdapderSoap(esbAtomservice));
    }

    @GetMapping("/updateEsbAdappderHttp")
    @ApiOperation("修改适配类型信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "serviceUrl", value = "目标地址"),
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "当前id"),
            @ApiImplicitParam(dataType = "String", name = "scharset", value = "发送字符编码"),
            @ApiImplicitParam(dataType = "String", name = "rcharset", value = "接收字符编码"),
            @ApiImplicitParam(dataType = "String", name = "httptype", value = "请求类型 1:get 2:post"),
    })
    public Response updateEsbAdappderHttp(HttpServletRequest request, Integer id, String serviceUrl, String scharset, String rcharset, String httptype) {
        if (isEmpty(serviceUrl) && isEmpty(rcharset) && isEmpty(scharset)){
            return Response.error("填写信息不能为空");
        }
        try {
            serviceUrl = URLDecoder.decode(serviceUrl,"utf-8");
        }catch(IOException e) {

        }
        if (!isEmpty(httptype)){
            EsbAdapderHttp esbAdapderHttp = new EsbAdapderHttp();
            esbAdapderHttp.setServiceUrl(serviceUrl);
            esbAdapderHttp.setRcharset(rcharset);
            esbAdapderHttp.setScharset(scharset);
            esbAdapderHttp.setHttptype(httptype);
            esbAdapderHttp.setId(id);
            userOperationLogService.savaUserLog(request, "新增或修改原子服务的http适配类型信息："+ esbAtomServiceDetailedService.
                    selectById(id).getAtomserviceCode());
            EsbAdapderHttp adapderHttp = esbAdapderHttpService.getById(id);
            if (isEmpty(adapderHttp)){
                return Response.success(esbAdapderHttpService.saveOrUpdate(esbAdapderHttp));
            }
            if (Objects.equals(adapderHttp.getServiceUrl(), serviceUrl) && Objects.equals(adapderHttp.getRcharset(), rcharset)  && Objects.equals(adapderHttp.getScharset(), scharset) && Objects.equals(adapderHttp.getHttptype(), httptype)){
                return Response.error("信息未变化");
            }else {
                return Response.success(esbAdapderHttpService.saveOrUpdate(esbAdapderHttp));
            }

        }
        EsbAdapderSoap esbAdapderSoap = new EsbAdapderSoap();
        esbAdapderSoap.setServiceUrl(serviceUrl);
        esbAdapderSoap.setRcharset(rcharset);
        esbAdapderSoap.setScharset(scharset);
        esbAdapderSoap.setId(id);
        userOperationLogService.savaUserLog(request, "新增或修改原子服务的soap适配类型信息："+ esbAtomServiceDetailedService.
                selectById(id).getAtomserviceCode());
        EsbAdapderSoap adapderSoap = esbAdapderSoapService.getById(id);
        if (isEmpty(adapderSoap)){
            return Response.success(esbAdapderSoapService.saveOrUpdate(esbAdapderSoap));
        }
        if (Objects.equals(adapderSoap.getServiceUrl(), serviceUrl) && Objects.equals(adapderSoap.getRcharset(), rcharset) && Objects.equals(adapderSoap.getScharset(), scharset)){
            return Response.error("信息未变化");
        }else {
            return Response.success(esbAdapderSoapService.saveOrUpdate(esbAdapderSoap));
        }
    }

}
